package tech.bmatch.photo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.photo.model.Photo;
import tech.bmatch.photo.service.PhotoService;
import tech.bmatch.photo.service.impl.PhotoServiceImpl;
import tech.bmatch.photo.util.FileUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class Application {
    /**
     * Logger
     */
    public static final Logger logger = LoggerFactory.getLogger(Application.class);

    /**
     * 静态服务
     */
    public static PhotoService photoService = new PhotoServiceImpl();

    /**
     * 用户ID
     */
    private static String userId;

    public static void main(String[] args) {
        logger.info("Start application.");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("请输入用户ID：");
        try {
            userId = bufferedReader.readLine();
        } catch (IOException e) {
            logger.error("用户ID初始化失败");
            e.printStackTrace();
        }
        System.out.println("当前用户ID：" + userId);

        while (true) {
            String command = null;
            try {
                command = bufferedReader.readLine();
            } catch (IOException e) {
                logger.error("", e);
            }

            if (command.startsWith("upload ")) {
                System.out.println("执行命令" + command);
                String filePath = command.replaceAll("upload ", "");
                uploadPhoto(filePath);
            }

            if (command.startsWith("list ")) {
                System.out.println("执行命令" + command);
                int pageNum = Integer.parseInt(command.replaceAll("list ", ""));
                query(pageNum);
            }

            if (command.startsWith("watermark ")) {
                System.out.println("执行命令" + command);
                command = command.replaceAll("watermark ", "");
                String[] splited = command.split("\\s+");
                String photoId = splited[0];
                int position = Integer.parseInt(splited[1]);
                addWatermark(photoId, position);
            }

            if (command.startsWith("clear ")) {
                System.out.println("执行命令" + command);
                String photoId = command.replaceAll("clear ", "");
                clearWatermark(photoId);
            }

            if (command.startsWith("exit")) {
                System.out.println("结束");
                break;
            }
        }

    }

    /**
     * 上传图片
     *
     * @param filePath 本地文件路径
     */
    public static void uploadPhoto(String filePath) {
        byte[] data = FileUtils.getData(filePath);
        //文件名称 带后缀
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        photoService.upload(data, fileName, userId);
    }

    /**
     * 查询图片
     *
     * @param pageNum 要获取的页码
     */
    public static void query(int pageNum) {
        List<Photo> photoList = photoService.query(pageNum, userId);
        if (photoList != null) {
            System.out.println("第" + pageNum + "页：");
            for (Photo photo : photoList) {
                System.out.println(photo.getId() + " " + photo.getName() + " " + photo.getStorePath());
            }
        }
    }

    /**
     * 添加水印
     * @param photoId 图片ID
     * @param position
     */
    public static void addWatermark(String photoId, int position) {
        photoService.doAddWatermark(photoId, position, userId);
    }

    /**
     * 清除水印
     * @param photoId 图片ID
     */
    public static void clearWatermark(String photoId) {
        photoService.doClearWatermark(photoId, userId);
    }

}
